package net.labdemo.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import net.labdemo.entity.Refer;
import net.labdemo.service.ReferService;

@Controller
@RequestMapping(value="/refer")
public class ReferController {
	@Autowired
	private ReferService referService;
	@ModelAttribute
	public Refer get(@RequestParam(required=false) String id){
		Refer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = referService.get(Long.valueOf(id));
		}
		if (entity == null){
			entity = new Refer();
		}
		return entity;
	}
	/*
	@RequestMapping(value="/add",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String add(Refer refer,Model model){
		if(referService.add(refer)==1){
			return new Gson().toJson(refer);
		}
		return "{ERROR}";
	}*/
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String form(
			@RequestParam("article")long article,
			Refer refer,
			Model model){
		model.addAttribute("articleId", article);
		model.addAttribute("refer", refer);
		return "refer/form";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String save(
			@ModelAttribute("refer")Refer referForm,
			@RequestParam("article")long article,
			Model model){
		if(referService.add(referForm, article)==1){
			return "redirect:/article/setting/"+article;
		}
		return "refer/form";
	}
}
